﻿/*********************************************************************************
**                                                                              **
**  Copyright (C) 2021-2025 LiLong                                              **
**  This file is part of CommonUnits.                                           **
**                                                                              **
**  CommonUnits is free software: you can redistribute it and/or modify         **
**  it under the terms of the GNU Lesser General Public License as published by **
**  the Free Software Foundation, either version 3 of the License, or           **
**  (at your option) any later version.                                         **
**                                                                              **
**  CommonUnits is distributed in the hope that it will be useful,              **
**  but WITHOUT ANY WARRANTY; without even the implied warranty of              **
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               **
**  GNU Lesser General Public License for more details.                         **
**                                                                              **
**  You should have received a copy of the GNU Lesser General Public License    **
**  along with CommonUnits.  If not, see <https://www.gnu.org/licenses/>.       **
**********************************************************************************/
#pragma once

#include "StandardUnit.h"

#if __has_include(<QString>)
#include <QString>
#endif

#include <format>

namespace CU
{
template<typename T>
constexpr bool _Always_false = std::false_type {};

template<typename _Type, UnitType _Unit>
requires std::is_arithmetic_v<_Type>
class CommonUnits;

template<typename T, typename _Type, UnitType _Unit>
inline void fromUnit(const CommonUnits<_Type, _Unit>& unit, T& dest)
{
    static_assert(_Always_false<T>, "Declare your fromUnit");
}

template<typename T, typename _Type, UnitType _Unit>
inline auto toUnit(_Type value, const T& from) -> CommonUnits<_Type, _Unit>
{
    static_assert(_Always_false<T>, "Declare your toUnit");
}

template<typename _Type, UnitType _Unit>
inline void fromUnit(const CommonUnits<_Type, _Unit>& unit, std::string& dest)
{
    dest = std::format("{}{}", unit.count(), unit.unit().data());
}

#if __has_include(<QString>)
template<typename _Type, UnitType _Unit>
inline void fromUnit(const CommonUnits<_Type, _Unit>& unit, QString& dest)
{
    dest = QString("%1%2", unit.count(), unit.unit().data());
}
#endif
} // namespace CU
